﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using Ext.Net;

public partial class ListUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            this.Store1.DataSource = this.GetDataReader();
            this.Store1.DataBind();
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        this.Store1.DataSource = this.GetDataReader();
        this.Store1.DataBind();
    }

    private SqlDataReader GetDataReader()
    {
        SqlConnection myConnection;
        SqlCommand myCommand;
        SqlDataReader myDataReader;

        string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SiyahBayrakConnectionString"].ConnectionString;

        myConnection = new SqlConnection(strConn);
        myConnection.Open();

        myCommand = new SqlCommand("listClients", myConnection);
        myCommand.CommandType = System.Data.CommandType.StoredProcedure;
        myDataReader = myCommand.ExecuteReader();

        return myDataReader;
    }

    [DirectMethod]
    public void viewClient(string command, int customerID)
    {
        if (command == "Edit")
        {
            idshower.Text = "Edit ID:"+customerID.ToString();
        }

        if (command == "Delete")
        {

            SqlConnection myConn;
            SqlCommand myComm;

            string strC = System.Configuration.ConfigurationManager.ConnectionStrings["SiyahBayrakConnectionString"].ConnectionString;
            myConn = new SqlConnection(strC);
            myConn.Open();

            myComm = new SqlCommand("deleteClient", myConn);
            myComm.CommandType = System.Data.CommandType.StoredProcedure;
            myComm.Parameters.AddWithValue("clientId", customerID);
            myComm.ExecuteNonQuery();
            myConn.Close();


            idshower.Text = "Selected customer record is deleted. Refresh grid.";
        }
    }
}